package com.CMUBiS.Depreciated;

import java.sql.*;

/**
 * DBTest.java - a database test class.
 * @author John Payne
 * @version 0.1
 * @date 04/03/2011
 * The DBTest Class is used to verify that your database
 * can be reached by the application server. 
 */

class DBTest { 
	private static Boolean verbose = true;
	private static String url = "jdbc:mysql://10.9.78.1/cmubis";
	private static String driver = "com.mysql.jdbc.Driver";
	private static String userName = "root"; 
	private static String password = "testtest";
    
	/** Runs each test for the database.
	 * 
	 */
	public static void main(String[] args) {
		Integer numberTests = 0;
		Integer successfulTests =0;
		System.out.println("Testing Database.\n");
		if(verbose){
			System.out.print("Testing Database Connectivity...  ");
    	}
		
		numberTests += 1;
	    Boolean testDBConnection = testConnection(verbose, driver, url,userName, password);
	    if(testDBConnection){successfulTests+=1;}
	    
	    System.out.print("\n"+successfulTests+"/"+numberTests+" Tests Successful.\n");
	    
	    registerUser("andy", "test");
	}
	
	/** Determines if the application server can connect to the the database.
	 * 
	 * @param verbose	If true details about tests are given
	 * @param driver	jdbc data base driver eg "com.mysql.jdbc.Driver"
	 * @param url		Database location eg "jdbc:mysql://IP_ADDRESS/DATABASE_NAME" 
	 * @param userName	username for database 
	 * @param password	password for database 
	 * @return			success (true) or failure (false) of connection test
	 */
	public static Boolean testConnection(Boolean verbose, String driver, String url, String userName, String password){
	  Connection conn = null;
	  Boolean success=true;
	  try {
		  Class.forName(driver).newInstance();
	      conn = DriverManager.getConnection(url,userName,password);
	      conn.close();
	      } catch (Exception e) {
	    	  success = false;
	    	if(verbose){
	    		System.out.print(e.getMessage()+"\n");
	    	}
	      }
      if(verbose&success){
    	  System.out.println("Database connected successfully!");
    	  }
      return success;
      }
	
	/**
	 * Registers a new user in the database
	 * @author Andy Turner
	 * @since 0.1
	 * @param username	The user name of the new user
	 * @param password	The password of the new user
	 * @return			0 if successful, 1 if username already exists
	 */
	public static int registerUser(String username2, String password2){
		Connection conn = null;
		
		// This is just junk code for now making sure the insert and stuff works
		try {
			  Class.forName(driver).newInstance();
		      conn = DriverManager.getConnection(url,userName,password);
		      
		      Statement statement;
		      
		      statement = conn.createStatement();
		      statement.executeUpdate("INSERT INTO users (firstname, lastname, username, password, email, region) VALUES ('first', 'last', 'user','pass','g@f.com',6)");
		      
		      
		      conn.close();
		 } catch (Exception e) {
		    e.printStackTrace();
		 }
		
		
		return 0;
	}
	} 